#include "object/building.h"
#include <common.h>
#include <filesystem.h>
#include <cvector.h>
#include <object/student.h>
#include <object/hostel.h>

#define HOSTELS_CFG_PATH \
    HOSTEL_MANAGER_HOME_DIR "hostels.cfg"

static cvector(Hostel) hostel_list = NULL;

HM_private
void save_hostels(void) {
    FILE *building_cfg = fopen(HOSTELS_CFG_PATH, "w");

    for (size_t i = 0; i < cvector_size(hostel_list); ++i) {
        fprintf(
            building_cfg,
            "%zu %zu %zu\n",
            hostel_list[i].hostel_id,
            hostel_list[i].building_id,
            hostel_list[i].bed_num
        );
    }
    fclose(building_cfg);
}

void register_hostel(size_t hostel_id, size_t building_id, size_t bed_num) {
    if (get_hostel(hostel_id, building_id)) {
        printf("hostel %zu %zu already exists\n", hostel_id, building_id);
        return;
    }
    Hostel hostel = {
        .hostel_id = hostel_id,
        .building_id = building_id,
        .bed_num = bed_num
    };
    cvector_push_back(hostel_list, hostel);
    save_hostels();
}

HM_private
void load_hostels(void) {
    if (crt_file_if_not_exist(HOSTELS_CFG_PATH)) {
        HM_throw(InternalError, "please bug report");
    }

    FILE *building_cfg = fopen(HOSTELS_CFG_PATH, "r");

    char buffer[16] = {0};

    while (read_line(building_cfg, buffer, sizeof(buffer))) {
        size_t hostel_id, building_id, bed_num;
        sscanf(buffer, "%zu %zu %zu", &hostel_id, &building_id, &bed_num);
        register_hostel(hostel_id, building_id, bed_num);
    }

    fclose(building_cfg);
}

int unregister_hostel(size_t hostel_id, size_t Building_id) {
    for (size_t i = 0; i < cvector_size(hostel_list); ++i) {
        if (hostel_list[i].hostel_id == hostel_id && hostel_list[i].building_id == Building_id) {
            cvector_erase(hostel_list, i);
            save_hostels();
            return 0;
        }
    }
    return 1;
}

int edit_hostel(size_t hostel_id, size_t building_id, size_t bed_num) {
    for (size_t i = 0; i < cvector_size(hostel_list); ++i) {
        if (hostel_list[i].hostel_id == hostel_id) {
            hostel_list[i].building_id = building_id;
            hostel_list[i].bed_num = bed_num;
            save_hostels();
            return 0;
        }
    }
    return 1;
}

void list_hostels(void) {
    for (size_t i = 0; i < cvector_size(hostel_list); ++i) {
        printf(
            "Hostel id: %zu, building id: %zu, bed num: %zu, used bed num: %zu\n",
            hostel_list[i].hostel_id,
            hostel_list[i].building_id,
            hostel_list[i].bed_num,
            count_students_in_a_hostel(hostel_list[i].hostel_id)
        );
    }
}

/* @return NULL if not found
 */
Hostel* get_hostel(size_t hostel_id, size_t Building_id) {
    for (size_t i = 0; i < cvector_size(hostel_list); ++i) {
        if (hostel_list[i].hostel_id == hostel_id &&
            hostel_list[i].building_id == Building_id) {
            return &hostel_list[i];
        }
    }
    return NULL;
}

void init_hostel_module(void) {
    crt_file_if_not_exist(HOSTELS_CFG_PATH);
    load_hostels();
}
